// modules/inform/pages/inform-page/inform-page.js
import {
  getMeassage
} from "@/api/inform"
import {
  getGroupDetail
} from "@/api/introduce"

Page({
  data: {
    message: [],
    informTitle: '我是标题',
    informContent: '暂无内容',
    group: '',
    informTime: 'null'
  },
  async onShow() {
    const {
      data
    } = await getMeassage();
    //console.log(data);

    for (let index = 0; index < data.length; index++) {
      const informItem = data[index];
      // 格式化时间
      data[index].publishTime = this.formattedTime(informItem.publishTime)
      // 请求组别name
      if (informItem.groupOp) {
        const res = await getGroupDetail(informItem.groupOp);
        data[index].groupName = res.data.name;
      }
    }

    this.setData({
      message: data,
    });
    console.log(this.data.message)
  },

  // 格式化时间
  formattedTime(timeStr) {
    let dateObj = new Date(timeStr);

    // 格式化日期
    let newTime = dateObj.getFullYear() + "-" +
      ("0" + (dateObj.getMonth() + 1)).slice(-2) + "-" +
      ("0" + dateObj.getDate()).slice(-2) + " " +
      ("0" + dateObj.getHours()).slice(-2) + ":" +
      ("0" + dateObj.getMinutes()).slice(-2) + ":" +
      ("0" + dateObj.getSeconds()).slice(-2);

    //console.log(newTime);
    return newTime
  }

})